#include <Config.h>

const char *ServerConfigPath = "./ServerConfig.json";
const char *SQLServerConfigPath = "./SQLServerConfig.json";

Config::Config(const char *path) {
    this->path = path;
    this->_success.first = true;
}

void Config::set_fail() {
    string _ = path;
    this->_success.first = false,
            this->_success.second = "配置文件 " + _ + " 不存在";
}

ServerConfig::ServerConfig(const char *path) : Config(path) {
    json json;
    ifstream config_file(path);
    if (config_file.is_open()) {
        config_file >> json;
        host = json.value("host", "0.0.0.0");
        listen_port = json.value("listen_port", 6666);
    }
    else {
        string _ = "配置文件";
        _ += path;
        _ += "不存在";
        this->set_fail();
    }
    config_file.close();
}

SQLServerConfig::SQLServerConfig(const char *path) : Config(path) {
    json json;
    ifstream config_file(path);
    if (config_file.is_open()) {
        config_file >> json;
        host = json.value("host", "127.0.0.1");
        port = json.value("port", 3306);
        username = json.value("username", "");
        password = json.value("password", "");
        database = json.value("database", "");
    }
    else {
        string _ = "配置文件";
        _ += path;
        _ += "不存在";
        set_fail();
    }
    config_file.close();
}

